<?php
require_once('dbinit.ini.php');
/**
 * Session类
 * 作用：对系统session进行了重写与继承，并于数据库的内存表中记录，用于解决高流量的情况
 *
 */
class Session
{
    private $mysqli;


    public function __construct() {
        $this->init();
    }

    public function init() {
        session_set_save_handler(array($this, 'open'),
                                 array($this, 'close'),
                                 array($this, 'read'),
                                 array($this, 'write'),
                                 array($this, 'destroy'),
                                 array($this, 'gc')
                                 );
    }

    public function open() {
        global $mysqli_write;
        $this->mysqli = $mysqli_write;
        return true;
    }

    public function close() {
        return true;
    }

    /**
     * Read the session
     * @param int session id
     * @return string string of the sessoin
     */
    public function read($id) {        
        $id = $this->mysqli->real_escape_string($id);
        if ($result = $this->mysqli->query("SELECT session_data FROM sessions WHERE session = '$id'")) {
            if ($row = $result->fetch_array(MYSQLI_ASSOC)) {
                return $row['session_data'];
            }
        }
        return '';
    }

    /**
     * Write the session
     * @param int session id
     * @param string data of the session
     */
    public function write($id, $data) {
        if ($data == '') return true;
        $id = $this->mysqli->real_escape_string($id);
        $data = $this->mysqli->real_escape_string($data);
        $time = time();
        $res = $this->mysqli->query("SELECT session FROM sessions WHERE session='$id'");
        if ($res && $res->num_rows>0) {
            $this->mysqli->query("UPDATE sessions SET session_last_write='$time', session_data='$data' WHERE session='$id'");
        } else {
            $this->mysqli->query("REPLACE INTO sessions (session,session_last_write,session_data,session_last_visit) VALUES ('$id', '$time', '$data', '$time')");
        }
    }

    /**
     * Destroy the session
     * @param int session id
     * @return bool
     */
    public function destroy($id) {
        $id = $this->mysqli->real_escape_string($id);
        return $this->mysqli->query("DELETE FROM sessions WHERE session = '$id'");
    }

    /**
     * Garbage Collector
     * @param int life time (sec.)
     * @return bool
     * @see session.gc_divisor      100
     * @see session.gc_maxlifetime 1440
     * @see session.gc_probability    1
     * @usage execution rate 1/100
     *        (session.gc_probability/session.gc_divisor)
     */
    public function gc($max) {
/*
        $expire = time() - $max;
        return $this->mysqli->query("DELETE FROM sessions WHERE session_last_write < '$expire'");
*/
    }
    
    public function visit($id) {
        $time = time();
        $id = $this->mysqli->real_escape_string($id);
        return $this->mysqli->query("UPDATE sessions SET session_last_visit='$time' WHERE session='$id'");
    }
    public function setUser($id, $user, $extended = '') {
        $id = $this->mysqli->real_escape_string($id);
        $user = $this->mysqli->real_escape_string($user);
        if ($extended !== '') {
            $extended = ($extended) ? 1 : 0;
            return $this->mysqli->query("UPDATE sessions SET userid='$user', extended='$extended' WHERE session='$id'");
        } else {
            return $this->mysqli->query("UPDATE sessions SET userid='$user' WHERE session='$id'");
        }
    }
}

global $gSession;
if ($_SERVER['HTTP_HOST'] !== 'admin.wzx.com') {
  ini_set('session.save_handler', 'user');
  $gSession = new Session();
}

?>
